package com.neusoft.service.impl;

import com.neusoft.core.Deal;
import com.neusoft.core.VirtualWallet;
import com.neusoft.mapper.DealMapper;
import com.neusoft.mapper.VirtualWalletMapper;
import com.neusoft.service.VirtualWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class VirtualWalletServiceImpl implements VirtualWalletService {
    @Autowired
    private VirtualWalletMapper virtualWalletMapper;
    @Autowired
    private DealMapper dealMapper;
    @Override
    public VirtualWallet getVirtualWallet(Integer walletId) {
        return virtualWalletMapper.getVirtualWalletById(walletId);
    }

    @Override
    public BigDecimal getBalance(Integer walletId) {
        return virtualWalletMapper.getVirtualWalletById(walletId).balance();
    }

    @Override
    public void debit(Integer walletId, BigDecimal amount) {
        VirtualWallet virtualWallet = virtualWalletMapper.getVirtualWalletById(walletId);
        virtualWallet.debit(amount);
        virtualWalletMapper.updateBalance(walletId, virtualWallet.balance());
    }

    @Override
    public void credit(Integer walletId, BigDecimal amount) {
        VirtualWallet virtualWallet = virtualWalletMapper.getVirtualWalletById(walletId);
        virtualWallet.credit(amount);
        virtualWalletMapper.updateBalance(walletId, virtualWallet.balance());
    }

    @Override
    @Transactional
    public void transfer(Integer fromWalletId, Integer toWalletId, BigDecimal amount) {
        System.out.println("开始钱包转账: 从" + fromWalletId + "到" + toWalletId + "，金额:" + amount);
        
        // Deal deal = new Deal();
        // deal.setAmount(amount);
        // deal.setFromWalletId(fromWalletId);
        // deal.setToWalletId(toWalletId);
        // deal.setStatus(0);
        
        try {
            // dealMapper.saveDeal(deal);
            // System.out.println("交易记录已创建，ID:" + deal.getDealId());
            
            debit(fromWalletId, amount);
            System.out.println("已从钱包" + fromWalletId + "扣款");
            
            credit(toWalletId, amount);
            System.out.println("已向钱包" + toWalletId + "入账");
            
            // dealMapper.updateStatus(deal.getDealId(), 1);
            // System.out.println("交易已完成");
        } catch (Exception e) {
            System.err.println("转账过程中出错: " + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public Integer saveVirtualWallet(String userId) {
        // 创建新的钱包实体
        VirtualWallet wallet = new VirtualWallet(0); // 0作为临时ID，会被数据库自增值替换
        wallet.setUserId(userId);
        wallet.setCreateTime(System.currentTimeMillis());
        wallet.setBalance(BigDecimal.ZERO);
        wallet.setAllowedOverdraft(true);
        wallet.setOverdraftAmount(BigDecimal.ZERO);
        wallet.setFrozenAmount(BigDecimal.ZERO);
        
        // 保存到数据库
        virtualWalletMapper.saveVirtualWallet(wallet); // 忽略返回的行数
        
        // 获取自动生成的钱包ID
        Integer walletId = wallet.getWalletId();
        
        System.out.println("创建钱包，用户ID: " + userId + "，生成的钱包ID: " + walletId);
        return walletId; // 返回实际的钱包ID
    }
}
